<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Controllers\common\MyAgentController;

use App\Http\Controllers\Controller;

use DB;
class UserController extends Controller{
    //用户列表
    public function getIndex(Request $request){
        //获取代理商id
        $where = array();
        $agentId = $request->cookie('agentId');
        $telephone = $request->input('phone','');
        if($telephone){
            $where['mobile'] = $telephone;
        }
        $username = $request -> input('name','');
        // dd($username);
        // $where['username']
        $get['ag'] = $request->input('ag',$agentId);
        $where['agent_id'] = $get['ag'];
        //查询父级代理商path
        $path = DB::table('sa_agent') -> select('path') -> where(['id'=>$agentId]) -> first();
        $len = substr_count($path['path'],',');
        $agent = MyAgentController::allAgent($agentId);
        foreach ($agent as $k=>$v){
            $agent[$k]['agent_name'] = str_repeat('--',substr_count($v['path'],',')-$len)."&nbsp;&nbsp;".$v['agent_name'];
        }
        //查询用户表和关系表
        $user = DB::table('sa_member as m')
            -> select('uid','username','mobile','telephone','weChat','reg_time','email')
            -> where($where)
            -> where('username','like','%'.$username.'%')
            -> paginate(10);
        $info = $user -> all();
        //dd($info);
        foreach ($info as $k=>$v){
            $con = ','.$v['uid'].',';
            $info[$k]['row'] = DB::table('sa_member_manager_area as mma')
                -> leftJoin('sa_area as a','a.a_id','=','mma.mma_a_id')
                -> leftJoin('sa_building as b','b.b_id','=','mma.mma_b_id')
                -> leftJoin('sa_room as r','r.r_id','=','mma.mma_r_id')
                -> select('mma.mma_a_id','a.a_name','mma.mma_b_id','b.b_name','mma.mma_r_id','r.r_name')
                -> where(DB::raw("concat(',',mma_uid,',')"),'like',"%$con%")
                -> get();
        }
//        dd($user);
        return view('admin.user.index',['agent'=>$agent,'user'=>$user,'info'=>$info,'request'=>$request->all(),'name'=>$username,'phone'=>$telephone,'get'=>$get]);
    }

    //添加用户信息页面
    public function getCreate(){
        return view('admin.user.create');
    }

    //添加用户处理方法
    public function postCreate(Request $request){
        $username = $request -> input('username');
        $telephone = $request -> input('telephone');
        $mobile = $request -> input('mobile');
        $weChat = $request -> input('weixin');
        $email = $request -> input('email');
        $reg_time = time();
        // $agent_id = session('agentId');
        $agent_id = $request->cookie('agentId');
        //判断用户是否已存在
        $row = DB::table('sa_member') -> where('username','=',$username) -> first();
        if($row){
            //存在
            $data['status'] = '-2';
            $data['msg'] = '用户名已存在';
        }else{
            //不存在
            //插入用户表
            $insertId = DB::table('sa_member') -> insertGetId(compact('username','telephone','mobile','weChat','email','reg_time','agent_id'));
            if($insertId){
                $data['status'] = '1';
                $data['msg'] = '用户添加成功';
            }else{
                $data['status'] = '-1';
                $data['msg'] = '用户添加失败';
            }
        }
        echo json_encode($data);
    }

    //判断用户是否存在
    public function postExists(Request $request){
        $username = $request -> input('username');
        //判断用户是否已存在
        $row = DB::table('sa_member') -> where('username','=',$username) -> first();
        if($row){
            $data['status'] = '-1';
            $data['msg'] = '用户名已存在';
        }else{
            $data['status'] = '1';
            $data['msg'] = '用户名可以添加';
        }
        echo json_encode($data);
    }

    //编辑用户信息页面
    public function getEdit(Request $request){
        //获取数据
        $uid = $request -> input('uid');
        //查询用户表
        $data = DB::table('sa_member') -> where('uid','=',$uid) -> first();
        return view('admin.user.edit',['data'=>$data]);
    }

    //编辑用户处理方法
    public function postEdit(Request $request){
        $uid = $request -> input('uid');
        $username = $request -> input('username');
        $mobile = $request -> input('mobile');
        $telephone = $request -> input('telephone');
        $weChat = $request -> input('weixin');
        $email = $request -> input('email');
        //修改用户表
        $row = DB::table('sa_member') -> where('uid','=',$uid) -> update(compact('username','mobile','telephone','weChat','email'));
        if($row){
            $data['status'] = '1';
            $data['msg'] = '用户修改成功';
        }else{
            $data['status'] = '-1';
            $data['msg'] = '用户修改失败';
        }
        echo json_encode($data);
    }
    // 用户删除
    public function getDelete(Request $request)
    {
        $id = base64_decode($request -> get('id'));
        $count = DB::table('sa_member_manager_area') -> where(DB::raw("concat(',',mma_uid,',')"),"like", "%,$id%,") -> count();
        if($count!=0){
            return back() -> withInput() -> with('error','该该客户有'.$count.'个管理职务，请解除后再进行删除!');
        }else{
            $delete = DB::table('sa_member') -> where('uid',$id) -> delete();
            if($delete){
                return redirect('/admin/user/index') -> with('success','删除成功');
            }
        }
    }
}
